package com.wht.controller.admin;

import com.wht.domain.Admin;
import com.wht.service.admin.AdminRegisterService;
import com.wht.service.admin.impl.AdminRegisterServiceImpl;
import com.wht.utils.MyBeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author wanghaotian
 * time:2022/11/21,0021 21:14
 * description:
 */

@WebServlet("/AdminRegisterController")
public class AdminRegisterController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String repassword = req.getParameter("repassword");
        if(password.equals(repassword)){
            Admin admin = new Admin(null,username,password);
            AdminRegisterService adminRegisterService = new AdminRegisterServiceImpl();
            int i = adminRegisterService.addAdmin(admin);
            if(i>0){
                resp.sendRedirect("/admin/login.jsp");
            }else {
                req.setAttribute("msg","注册失败");
                req.getRequestDispatcher("/msg.jsp").forward(req, resp);
            }
        }else {
            req.setAttribute("msg","两次密码输入不一致");
            req.getRequestDispatcher("/msg.jsp").forward(req, resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}